package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.sax.Element;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ElementActivity extends AppCompatActivity {
    private TextView ZfNumber,ZFName;
    private Button ElementBtn,ElementBnt1;
    private String user_id,tunnel_code,money,money1;
    private RecyclerView recyclerView;
    private JSONArray jsonObject1;
    private ImageButton Elementfh;
    private int a;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wz_activity_element);
        //money
        ZfNumber = findViewById(R.id.ZfNumber);
        ZFName = findViewById(R.id.ZFName);

        Intent intent = getIntent();
        String kjName = intent.getStringExtra("kjName");
        money = intent.getStringExtra("money");
        money1 = intent.getStringExtra("money1");
        tunnel_code = intent.getStringExtra("tunnel_code");
        //添加金额
        ZfNumber.setText(money);
        //通道名称
        ZFName.setText(kjName.trim());

        init();
        //添加信用卡
        ElementBtn = findViewById(R.id.ElementBtn);
        ElementBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ElementActivity.this,AddCardActivity.class);
                intent.putExtra("cardtype","2");
                startActivity(intent);
            }
        });
        //提交
        ElementBnt1 = findViewById(R.id.ElementBnt1);
        ElementBnt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tiaozhuan();
//
            }
        });
        //返回
        Elementfh = findViewById(R.id.ElementFh);
        Elementfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        final RefreshLayout refreshLayout2 = (RefreshLayout)findViewById(R.id.ElementLayout);
        refreshLayout2.setOnRefreshListener(new OnRefreshListener() {
            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                            Toast.makeText(ElementActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                            refreshLayout2.finishRefresh();
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
    }

    public void init(){
        Object user_info = Preferences.getParam(ElementActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());
            //账号
            user_id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1012(user_id, "2", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    jsonObject1 = (JSONArray) jsonObject.get("data");
                    recyclerView = findViewById(R.id.Elementtab);
                    recyclerView.setLayoutManager(new LinearLayoutManager(ElementActivity.this));
                    recyclerView.setAdapter(new ElementAdapter(ElementActivity.this, jsonObject1,recyclerView,ElementActivity.this));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                showToast(ElementActivity.this,"网络错误");
            }
        });
    }

    public void tiaozhuan(){
        //showToast(ElementActivity.this,a+"");
        try {
            final JSONObject jsb = (JSONObject) jsonObject1.get(a);
            OKManager.getInstance().type1013(user_id, tunnel_code, money1, jsb.get("cardnumber").toString(), new OKManager.func4() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        if(jsonObject.get("code").toString().trim().equals("0")){
                            Intent intent = new Intent(ElementActivity.this,UnionpayActivity.class);
                            intent.putExtra("user_id",user_id);
                            intent.putExtra("txnAmt",money1);
                            intent.putExtra("tunnel_code",tunnel_code);
                            intent.putExtra("accNo",jsb.get("cardnumber").toString());
                            startActivity(intent);
                        }else{
                            showToast(ElementActivity.this,jsonObject.get("message").toString());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(String string) {
                    showToast(ElementActivity.this,"网络错误");
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public void setA(int a){
        this.a = a;
    }
}
